package com.web.new12306.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.web.new12306.pojo.Ticket;
import com.web.new12306.service.TicketService;
import com.web.new12306.service.impl.TicketServiceImpl;

/**
 * Servlet implementation class AddTicket
 */
/**
 * 封梦琳
 */
@WebServlet("/AddTicket")
public class AddTicket extends HttpServlet {
	private static final long serialVersionUID = 1L;
  
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		doInsert(request, response);
	}
		protected void doInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		System.out.println("11");
		
		Ticket t=new Ticket();
		String trainNumber=request.getParameter("trainNumber");
		String startStation=request.getParameter("startStation");
		String terminalStation=request.getParameter("terminalStation");
		String date=request.getParameter("time");
		String price=request.getParameter("price");
		String remainingTickets=request.getParameter("remainingTickets");
	
	   
		System.out.println(date);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd 24HH:mm:ss");
		//Date time;
		PrintWriter out=response.getWriter();
		TicketService ts=null;
		try {
			ts=new TicketServiceImpl();
		//	time = sdf.parse(date);
			try {
				int prices=Integer.parseInt(price);
				int  remainingTicket=Integer.parseInt(request.getParameter("remainingTickets"));
				
				t.setTrainNumber(trainNumber);
				t.setStartStation(startStation);
				t.setTerminalStation(terminalStation);
			//	t.setTime(new Timestamp(time.getTime()));
				t.setPrice(prices);
				t.setRemainingTickets(remainingTicket);
				ts.insertTicket(t);
				out.print("<script>alert('添加成功')</script>");
			}catch(NumberFormatException e)
			{
				out.print("<script>alert('添加失败')</script>");
			}
		} finally {
			//catch (ParseException e) {
		}
			// TODO Auto-generated catch block
			//e.printStackTrace();
		//}
		
	}
	}

